import { BasicColumn, FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { Icon } from '/@/components/Icon';

export const columns: BasicColumn[] = [
  // { title: 'id', dataIndex: 'id' },
  {
    title: '菜单名称',
    dataIndex: 'title',
    width: 200,
    align: 'left',
  },
  {
    title: '排序',
    dataIndex: 'sort',
    edit: true,
    editable: true,
    editComponent: 'InputNumber',
    editComponentProps: { size: '' },
    width: 140,
  },
  {
    title: '图标',
    dataIndex: 'icon',
    width: 50,
    customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },
  },
  {
    title: '路由地址',
    dataIndex: 'router',
    width: 100,
  },
  {
    title: '权限标识',
    dataIndex: 'nodes',
    customRender: ({ record }) => {
      const arr = record.nodes
        ?.map((item: any) => {
          const _item = [...item];
          const method = _item.pop().split(' ');
          return _item.join('/') + (method[0] ? `/${method[0]}` : '') + ` ${method[1]}`;
        })
        .map((item: any) => h('div', {}, item));
      return h('div', {}, arr);
    },
    width: 200,
  },
  {
    title: '组件',
    dataIndex: 'viewPath',
  },
  {
    title: '状态',
    dataIndex: 'isShow',
    width: 80,
    customRender: ({ record }) => {
      const color = record.isShow ? 'green' : 'red';
      const text = record.isShow ? '显示' : '隐藏';
      return h(Tag, { color: color }, () => text);
    },
  },
];

// const isDir = (type: number) => type === 0;
const isMenu = (type: number) => type === 1;
const isButton = (type: number) => type === 2;

export const searchFormSchema: FormSchema[] = [
  {
    field: 'title',
    label: '菜单名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '停用', value: '1' },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
  },
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '目录', value: 0 },
        { label: '菜单', value: 1 },
        { label: '按钮', value: 2 },
      ],
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'title',
    label: '菜单名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    defaultValue: 0,
  },
  {
    field: 'router',
    label: '路由地址',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'parentId',
    label: '上级菜单',
    required: true,
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'title',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    colProps: { lg: 24, md: 24 },
  },
  {
    field: 'viewPath',
    label: '组件路径',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => isMenu(values.type),
  },
  {
    field: 'keepalive',
    label: '是否缓存',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '否', value: false },
        { label: '是', value: true },
      ],
    },
    ifShow: ({ values }) => isMenu(values.type),
  },

  {
    field: 'isShow',
    label: '是否显示',
    component: 'RadioButtonGroup',
    defaultValue: true,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'nodes',
    label: '权限标识',
    component: 'Cascader',
    componentProps: {
      multiple: true,
      showCheckedStrategy: 'SHOW_CHILD',
      displayRender: ({ labels }) => {
        return labels.join('/');
      },
    },
    colProps: { lg: 24 },
    ifShow: ({ values }) => isButton(values.type),
    required: ({ values }) => isButton(values.type),
  },
  {
    field: 'addNodes',
    label: '添加按钮',
    helpMessage: ['注意:', '选择此项将成生成[添加][修改][列表][删除]按钮节点'],
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '是', value: true },
        { label: '否', value: false },
      ],
    },
    ifShow: ({ values }) => !values.id && isMenu(values.type),
  },
];
